<template>
  <div @click="showDialog">
    <slot/>
    <el-dialog v-model="resetDialogVisible" appendToBody title="" width="580px">
      <div class="padding-0-40">
        <h2>忘记密码？</h2>
        <el-form label-width="80px" ref="formRef" :model="formData.form" :rules="rules">
          <p class="margin-30-0">请输入您用于登录账户的电子邮件地址。</p>
          <el-form-item label="邮件地址：" required prop="email">
            <el-input type="password" v-model="formData.form.email" placeholder="请输入登录邮件地址">
              <template #prefix>
                <svg-icon icon="mail" width="22px" height="22px" />
              </template>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="padding-40">
          <el-button @click="submit(formData.form)" :loading="loading" type="primary" class="full">
            重置密码
          </el-button>
        </div>
      </template>
    </el-dialog>


    <el-dialog v-model="resetDialogResultVisible" appendToBody title="" width="580px">
      <div class="padding-0-40">
        <h2>发送重置密码</h2>
        <div style="background: #F5F5F6; border-radius: 4px; padding: 12px 20px;">
          <h3 class="margin-0"><svg-icon icon="success" width="22px" height="22px" />发送成功！</h3>
          <p>密码已重置，请查收邮箱。</p>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script lang="ts">

import {Options} from "vue-class-component";
import BasePage from "../pages/BasePage";
import {useRoute, useRouter} from "vue-router";
import {reactive, ref} from "vue";
import {storeToRefs} from "pinia";
import {useUser} from "@/store/user";

@Options({
  name: 'LoginDialog'
})
export default class ForgetPwdDialog extends BasePage {
  router = useRouter()
  route = useRoute()

  resetDialogVisible = false
  resetDialogResultVisible = false
  //登录相关
  useUser = useUser()
  user = storeToRefs(useUser())?.userInfo

  previewIndex = -1

  preview = {}

  confirmPwd = ''

  formData = reactive({
    form: {
      email: ''
    },
  })

  // 获取页面组件对象
  formRef = ref();
  rules = {
    username: [
      {required: true, message: '必须输入用户名', trigger: 'blur'},
    ],
    password: [
      {required: true, message: '必须输入密码', trigger: 'blur'},
    ]
  }

  // 传递参数的方式
  async submit(form) {
    try {
      this.loading = true
      await this.formRef.validate()
      // let res = await loginAPI(form);
      // if (res.code === '000') {
      //   this.resetDialogResultVisible = false
      // } else {
      //   ElMessage.error(res.errMessage)
      // }
      this.resetDialogVisible = false
      this.resetDialogResultVisible = true
    } catch (err) {
      console.log(err)
    }
    this.loading = false
  }

  onResetSuccess() {
    this.goPage('/')
  }
  showDialog() {
    this.resetDialogVisible = true
  }

  goPage(path: string) {
    this.$router.push({path})
  }

}
</script>

<style lang="scss" scoped>
.el-input, .el-button {
  height: 48px;
}
:deep(.el-form-item__label) {
  display: none;
}
</style>